package com.ccnu.sm.jdbc;/*
 * @author raohj
 * @date 2019/11/1014:33
 * @description add your comment
 */

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class RCustDaoImpl implements RCustDao {
    private JdbcTemplate jdbcTemplate;

    public RCustDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int selectCount() {
        return jdbcTemplate.queryForObject("select count(*) from t_cust", Integer.class);
    }

    @Override
    public int insert(RCust cust) {
        return jdbcTemplate.update("INSERT INTO rhjdb.t_cust (CUST_NO, CUST_TYPE, CUST_NAME, ID_TYPE, ID_CODE, BANK_ACCT) VALUES (?, ?, ?, ?, ?, ?)",
                cust.getCustNo(),
                cust.getCustType(),
                cust.getCustName(),
                cust.getIdType(),
                cust.getIdCode(),
                cust.getBankAcct());
    }

    @Override
    public List<RCust> select() {
        return jdbcTemplate.query("select * from t_cust",
                new RowMapper<RCust>() {
                    @Override
                    public RCust mapRow(ResultSet rs, int rowNum) throws SQLException {
                        RCust cust = new RCust();
                        cust.setCustNo(rs.getString("cust_no"));
                        cust.setCustType(rs.getString("cust_type"));
                        cust.setCustName(rs.getString("cust_name"));
                        cust.setIdType(rs.getString("id_type"));
                        cust.setIdCode(rs.getString("id_code"));
                        cust.setBankAcct(rs.getString("bank_acct"));
                        return cust;
                    }
                });
    }

    @Override
    public RCust selectById(String custNo) {
        return jdbcTemplate.queryForObject("select * from t_cust where cust_no=?",
                new Object[]{custNo},
                new RowMapper<RCust>() {
            @Override
            public RCust mapRow(ResultSet rs, int rowNum) throws SQLException {
                RCust cust = new RCust();
                cust.setCustNo(rs.getString("cust_no"));
                cust.setCustType(rs.getString("cust_type"));
                cust.setCustName(rs.getString("cust_name"));
                cust.setIdType(rs.getString("id_type"));
                cust.setIdCode(rs.getString("id_code"));
                cust.setBankAcct(rs.getString("bank_acct"));
                return cust;
            }
        });
    }

    @Override
    public int upate(RCust cust) {
        return jdbcTemplate.update("update t_cust set cust_name=? where cust_no=?", cust.getCustName(), cust.getCustNo());
    }

    @Override
    public int deleteById(String custNo) {
        return jdbcTemplate.update("delete from t_cust where cust_no=?", custNo);
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}
